package com.example.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.example.entity.Video;
import com.example.service.VideoService;
import com.example.vo.VideoVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author: hx
 * @CreateTime: 2022/3/7 15:03
 * @Description:
 * */
@RestController
@Slf4j
public class VideoServiceController {
    @Autowired
    private VideoService videoService;

    @PostMapping("/publish")
    public Video publish(@RequestBody Video video) throws JsonProcessingException {
        log.info("接受到的视频：{}",new ObjectMapper().writeValueAsString(video));
        return videoService.insert(video);
    }

    /**
     * 根据视频id查询视频的方法
     */
    @GetMapping("/getVideos")
    public List<VideoVO> getVideos(@RequestParam("ids") List<Integer> ids){
        ArrayList<VideoVO> videoVOS = new ArrayList<>();
        ids.forEach(id -> {
            VideoVO videoVo = videoService.queryById(id);
            videoVOS.add(videoVo);
        });
        return videoVOS;
    }
}
